﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Microsoft.Practices.Unity;
using MiniStock.BusinessLogic.EventDispatcher;
using MiniStock.BusinessLogic.EventDispatcher.MessageMap;
using MiniStock.BusinessLogic.EventQueue.Queue;
using MiniStock.Entities.Stock;
using MiniStock.Infrastructure;
using MiniStock.Infrastructure.Configurator;
using MiniStock.Infrastructure.Queue;

namespace MiniStockEventDispatcherHost
{
    internal static class Program
    {
        public static void ConfigureGlobalContainer(IUnityContainer container)
        {
            container.RegisterType<IEventQueue, MsmqQueue>();
            container.RegisterType<IEventQueue, MsmqQueue>("InternalQueue");

            container.RegisterType<IPluginConfigurator, ConfigBasedConfigurator>(
                new ContainerControlledLifetimeManager());
            container.RegisterType<IEventDispatcherConfigurator, ConfigBasedConfigurator>(
                new ContainerControlledLifetimeManager());
            container.RegisterType<IEventProcessorConfigurator, ConfigBasedConfigurator>(
                new ContainerControlledLifetimeManager());
            container.RegisterType<IEventQueueConfigurator, ConfigBasedConfigurator>(
                new ContainerControlledLifetimeManager());
            container.RegisterType<IEventQueueConfigurator, ConfigBasedConfigurator>("InternalConfigurator",
                                                                                     new ContainerControlledLifetimeManager
                                                                                         ());
            container.RegisterType<IEntityPersisterConfigurator, ConfigBasedConfigurator>(
                new ContainerControlledLifetimeManager());
        }

        public static void ConfigureContainer(IUnityContainer container)
        {
            container.RegisterType<EventDispatcherLogic>(new ContainerControlledLifetimeManager());
            container.RegisterType<EventDispatcherWorker>(new ExternallyControlledLifetimeManager());
            container.RegisterType<IRepository<MessageMapRepositoryEntry>, MessageMapDbRepository>();

            // load customer plugin dll
            IProcessorPluginEntryPoint p = new PluginEntryPoint();
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static void Main()
        {
            MiniStockContext.OnConfigureGlobalContainer = ConfigureGlobalContainer;
            MiniStockContext.OnConfigureContainer = ConfigureContainer;

            using (var scope = new MiniStockContextScope())
            {

                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                                    {
                                        new EventDispatcherService()
                                    };
                ServiceBase.Run(ServicesToRun);
            }
        }
    }
}